%% by yanchun.li@smail.hust.edu.cn
clear;close all;

%% input
X=[-1:0.5:2].';
Y=[-4.447 -0.452 0.551 0.048 -0.447 0.549 4.552].';
K=3;
W=1;

%% Polynomial Curve Fitting 
%Method_Name='Polynomial';
Method_Name='Orth-Polynomial';
if strcmp(Method_Name,'Polynomial')
    % Polynomial Curve Fitting
    [A]=nature_function_curvefitting(X,Y,K,W);
    base_val = nature_base_func(X,K);
    Y_approx=base_val*A;
    
    %% Calculate the curve
    X_OverSample=-1.2:0.1:2.2;
    base_val = nature_base_func(X_OverSample,K);
    Y_approx_OverSample=base_val*A;
else
    % Orth-Polynomial Curve Fitting
    [A]=orth_function_curvefitting(X,Y,K,W);
    Y_approx = orth_base_func2(A,X);
    
    %% Calculate the curve
    X_OverSample=[-1.2:0.1:2.2].';
    Y_approx_OverSample = orth_base_func2(A,X_OverSample);
end



%% Display Results
figure;hold on;
h=plot(X,Y,'r+');
set(h,'DisplayName','Original Date');
h=plot(X,Y_approx,'gx');
set(h,'DisplayName','Curve Fitting on Original Sample Point');
h=plot(X_OverSample,Y_approx_OverSample,'b-');
set(h,'DisplayName','Curve Fitting');
legend('Show');
legend('Location','Northwest');
xlabel('X');
ylabel('Y');
title('approximation by polynomial');

figure;
Y_err = Y-Y_approx;
bar(X,Y_err);
xlabel('X');
ylabel('Y - Y''s approximation');
title('error of approximation at each sample point');

err_var = sum(Y_err.^2)